package ooo.reindeer.commons.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

public class DateUtils {

    static final ThreadLocal<Map<String, SimpleDateFormat>> localDateFormat = new ThreadLocal<Map<String, SimpleDateFormat>>();

    public static int compareTo(String format, Date date, String beginDate, String endDate) {
        String nowDate = DateUtils.getDateTime(format, date);
//		LogUtils.info("DateUtils compareTo :"+format+" ret:"+nowDate);
        if ((beginDate == null || beginDate.isEmpty())) {
            beginDate = nowDate;
        }

        if (endDate == null || endDate.isEmpty()) {
            endDate = nowDate;
        }

        if ((nowDate.equals(beginDate) && nowDate.equals(endDate))) {
            return 0;
        } else {

            long tn = DateUtils.toMillionSeconds(format, nowDate);
            long tb = DateUtils.toMillionSeconds(format, beginDate);
            long te = DateUtils.toMillionSeconds(format, endDate);

            if (tn < tb) {
                return -1;
            } else if (tn > (te) && !nowDate.equals(endDate)) {
                return 1;
            } else {
                return 0;
            }
        }
    }

    private static SimpleDateFormat getDateFormat(String format) {
        Map<String, SimpleDateFormat> dateMap = localDateFormat.get();
        if (dateMap == null) {
            dateMap = new HashMap<String, SimpleDateFormat>();
        }
        SimpleDateFormat dateFormat = dateMap.get(format);
        if (dateFormat == null) {
            dateFormat = new SimpleDateFormat(format);
            dateMap.put(format, dateFormat);
        }
        return dateFormat;
    }

    public static final String getDateTime() {
        return getDateTime("yyyy-MM-dd HH:mm:ss", new Date());
    }

    public static String getDateTime(String destDateFormat, long millionSeconds) {

        return getDateTime(destDateFormat, new Date(millionSeconds));
    }

    public static String getDateTime(String destDateFormat, String dateFormat, String date) {
        return getDateTime(destDateFormat, getDateTime(dateFormat, date));
    }

    public static Date getDateTime(String string, String requestTime) {
        try {
            return getDateFormat(string).parse(requestTime);
        } catch (ParseException e) {
            return new Date();
        }
    }

    public static String getDateTime(String format) {
        return getDateTime(format, new Date());
    }

    public static String getDateTime(String format, Object date) {
        if (date instanceof Date) {
            return getDateTime(format, (Date) date);
        } else {
            return getDateFormat(format).format(date);
        }

    }

    public static String getDateTime(String format, Date date) {

        return getDateFormat(format).format(date);
    }

    public static String getDateTimeByMinute(long minutes) {
        return DateUtils.getDateTimeByMinute("yyyy-MM-dd HH:mm:ss.s", minutes);
    }

    public static String getDateTimeByMinute(String format, long minutes) {
        long pc = DateUtils.toMillionSeconds("HHmmss", "000000");
        return DateUtils.getDateTime(format, new Date((minutes * 60 * 1000 + pc)));
    }

    public static final String getDay(Date date) {
        return getDateTime("dd", date);
    }

    public static final String getIsoDateString() {
        return getIsoDateString(new Date());
    }

    public static final String getIsoDateString(Date date) {
        return getDateTime("yyyy-MM-dd", date);
    }

    public static long getMinuteByDateTime(String format, String dateTime) {
        long pc = DateUtils.toMillionSeconds("HHmmss", "000000");
        long cu = DateUtils.toMillionSeconds(format, dateTime);
        return (cu - pc) / 1000 / 60;
    }

    public static final String getMonth(Date date) {
        return getDateTime("MM", date);
    }

    public static final String getYear(Date date) {
        return getDateTime("yyyy", date);
    }

    public static long toMillionSeconds(String format, String date) {
        long millionSeconds;
        try {
            millionSeconds = getDateFormat(format).parse(date).getTime();
        } catch (ParseException e) {
            throw new RuntimeException(e);
        }
        return millionSeconds;
    }

//	public static void main(String[] args) {
//		System.out.println(getDateTime());
//		System.out.println(getDateTime("yyyy-MM-dd HH:mm:ss.SSS"));
//		System.out.println(getDateTime("yyyy-MM-dd HH:mm:ss.SSS",new Date()));
//		System.out.println(getDateTime("yyyy-MM-dd HH:mm:ss.SSS","yyyy-MM-dd","2017-09-10"));
//		System.out.println(toMillionSeconds("HHmmss", "080000"));
//		System.out.println(compareTo("yyyy-MM-dd",new Date(),"2017-07-04","2017-07-04"));
//		System.out.println(compareTo("yyyy-MM-dd",new Date(),"2017-07-03","2017-07-04"));
//		System.out.println(compareTo("yyyy-MM-dd",new Date(),"2017-07-08","2017-07-09"));
//		System.out.println(compareTo("yyyy-MM-dd",new Date(),"2017-07-04","2017-07-03"));
//		System.out.println(compareTo("yyyy-MM-dd",new Date(),"2017-07-03","2017-07-03"));
//		System.out.println(compareTo("yyyy-MM-dd",new Date(),"2017-07-02","2017-07-03"));
//		
//		System.out.println(compareTo("yyyy-MM-dd",new Date(),"",""));
//		System.out.println(compareTo("yyyy-MM-dd",new Date(),"","2017-07-04"));
//		System.out.println(compareTo("yyyy-MM-dd",new Date(),"2017-07-04",""));
//		System.out.println(compareTo("yyyy-MM-dd",new Date(),"2017-07-05",""));
//		System.out.println(compareTo("yyyy-MM-dd",new Date(),"","2017-07-05"));
//		System.out.println(compareTo("yyyy-MM-dd",new Date(),"","2017-07-03"));

//	System.out.println(getDateTimeByMinute("HHmmss",1440));
//	System.out.println(getMinuteByDateTime("HHmmss","235959"));
//	String hour;
//	hour=DateUtils.getDateTimeByMinute("HHmmss",Long.parseLong("0"));
//	System.out.println(hour);
//	String scheduleTime=DateUtils.getDateTime("yyyyMMdd", "yyyy-MM-dd HH:mm:ss.S", "2017-07-15 15:24:34.12")+hour;
//	System.out.println(scheduleTime);
//		System.out.println(DateUtils.compareTo("yyyy-MM-dd HH:mm", new Date(),
//				"2017-07-27 16:21:00", "2017-07-27 16:22:00"));

//	}

}
